package org.example.springbootproject.service.impl;

import org.example.springbootproject.mapper.OutboundMapper;
import org.example.springbootproject.pojo.Goods;
import org.example.springbootproject.pojo.OutGoods;
import org.example.springbootproject.pojo.Outbound;
import org.example.springbootproject.pojo.Result;
import org.example.springbootproject.service.OutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
@Service
public class Outboundimpl implements OutboundService {
    @Autowired
    private OutboundMapper outboundMapper;

    @Override
    public List<Outbound> findOutbound() {
        return outboundMapper.findOutbound();
    }

    @Override
    public Result upDate(OutGoods outGoods) {
        Double outboundton = outGoods.getOutboundTon();
        LocalDate sailingDay = outGoods.getSailingDay();
        int goodsCount = outboundMapper.findGoodsCount();
        Goods tempGoods = outboundMapper.findGoods();
        double fees = 0.0;

        if(goodsCount == 1 && outboundton > tempGoods.getInboundTonnage()){
            return Result.error("库存不足");
        }

        while (outboundton > 0){
            tempGoods = outboundMapper.findGoods();
            Integer tempId = outboundMapper.getFirstId();
            if(tempGoods == null){
                return Result.error("库存不足");
            }

            Double tempTon = tempGoods.getInboundTonnage();
            if(tempTon > outboundton){
                long betweenDays = ChronoUnit.DAYS.between(tempGoods.getStorageTime(), sailingDay);
                betweenDays += 1L;

                if(betweenDays > 14L){
                    fees += outboundton * (betweenDays - 14L) * 0.5;
                }
                tempTon = tempTon - outboundton;
                outboundton = 0.0;

                outboundMapper.updateGoods(tempTon,tempId);
            } else if(tempTon < outboundton){
                long betweenDays = ChronoUnit.DAYS.between(tempGoods.getStorageTime(), sailingDay);
                betweenDays += 1L;

                if(betweenDays > 14L){
                    fees += tempTon * (betweenDays - 14L) * 0.5;
                }

                outboundton = outboundton - tempTon;
                outboundMapper.deleteGoods();
            }
            else {
                long betweenDays = ChronoUnit.DAYS.between(tempGoods.getStorageTime(), sailingDay);
                betweenDays += 1L;

                if(betweenDays > 14L){
                    fees += tempTon * (betweenDays - 14L) * 0.5;
                }
                outboundton = 0.0;
                outboundMapper.deleteGoods();
            }
        }

        String OrderNumber = "ORD-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
        String ladingNumber = "LAD-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
        String businessId = "BUS-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
        Double Allremainton = outboundMapper.getremainton();

        // 如果 Allremainton 为 null，则赋值为 0
        if (Allremainton == null) {
            Allremainton = 0.0;
        }
        outboundMapper.addOutbound(sailingDay,OrderNumber,outGoods.getOutboundQty(),outGoods.getOutboundTon(),ladingNumber,businessId,fees,Allremainton);

        return Result.success();
    }
}
